<?php
    session_start();
    include_once '../../../../controlador/php/conexion/Conexion.php';
    $con = new Conexion();
    
    $oid = $_REQUEST['oid'];
    $obs = utf8_decode($_REQUEST['obs']);
    $usuario = $_SESSION['usuario'];

        $count ="select COUNT(*) as n,solcita from segimagenes where hcnsolexa = '".$oid."' group by solcita";
        $res = $con->query($count);
    
    while($res2 = mssql_fetch_array($res)){
        $n = $res2['n'];
        $solcita = $res2['solcita'];
    }
    if ($n == 0){
        
        if($usuario == ""){
            
            print_r(json_encode(array('n'=>1)));
            
        }
        else{
            
            $inser= "insert into segimagenes (hcnsolexa,segfecindi,segestado,usu_oid,solcita,fecsolcita,usu_oidsc,obsesolcita)values('".$oid."','','','','1',GETDATE(),'".$_SESSION['usuario']."','".$obs."')";
            $con->query($inser);
            print_r(json_encode(array('n'=>2)));
        }
    }
    else {
        if($usuario == ""){
            
            print_r(json_encode(array('n'=>1)));
        }
        else{
            if($solcita != 1){
                $Editar = ', fecsolcita = GETDATE()';
            }else{
                $Editar = '';
            }
             $update ="update segimagenes
                        set solcita ='1',
                        obsesolcita = '".$obs."',
                        usu_oidsc ='".$_SESSION['usuario']."'
                        $Editar
                      where hcnsolexa ='".$oid."'";
            $con->query($update);
        print_r(json_encode(array('n'=>2)));
        }
   }
       
    $con->cerrarConexion();
    
 
?>